using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using BoOnce.Components;
using BoOnce.Steps;

namespace BoOnce.Menu
{
    class MenuScreen
    {
        public enum ScreenStates { TransitionIn, TransitionOut, TransitionFinished };

        private const double NOT_INIT = -1;

        private ScreenStates _State = ScreenStates.TransitionIn;
        private double _TransitionOutDuration = 200; //ms
        private double _TransitionInDuration = 300; //ms
        private double _TransitionStartAt = NOT_INIT;
        private float _TransitionProgress = 0.0f;

        private float _Rot = 0f;
        private float _Scale = 1f;
        private byte _Alpha = 255;

        private Vector2 _TitlePosition;
        private Vector2 _TitleMarginBottom;
        private Vector2 _ItemMarginBottom;
        private int _HighlightedEntryIndex;
        private MenuItem _HighlightedEntry;

        private String _Title;
        private LinkedList<MenuItem> _Items;
        private SpriteBatch _SpriteBatch;
        private SpriteFont _Font;
        
        protected Menu _Menu;

        #region Properties
        public ScreenStates State { get { return _State; } }
        protected SpriteBatch SpriteBatch { get { return _SpriteBatch; } }
        #endregion

        public MenuScreen(Menu menu, String title)
        {
            _Menu = menu;
            _HighlightedEntryIndex = 0;
            _SpriteBatch = new SpriteBatch(BaseGame.GetInstance().GraphicsDevice);
            _TitlePosition = new Vector2(50, 50);
            _TitleMarginBottom = new Vector2(0, 100);
            _ItemMarginBottom = new Vector2(0, 30);
            _Title = title;
            _Items = new LinkedList<MenuItem>();
        }

        protected void Add(MenuItem item)
        {
            _Items.AddLast(item);
            if (_Items.Count == 1)
                HighlightEntry(0);
        }

        private void HighlightEntry(int index)
        {
            index = index % (_Items.Count + 1);
            LinkedList<MenuItem>.Enumerator e = _Items.GetEnumerator();
            int i = 0;
            foreach (MenuItem entry in _Items)
            {
                entry.IsHightlighted = false;
                if (i == index)
                {
                    entry.IsHightlighted = true;
                    _HighlightedEntry = entry;
                }
                i++;
            }
        }

        public void SelectHighlightedEntry()
        {
            _HighlightedEntry.OnSelectEntry();
        }

        public void HighlightNextEntry()
        {
            _HighlightedEntryIndex++;
            _HighlightedEntryIndex = _HighlightedEntryIndex % _Items.Count;
            HighlightEntry(_HighlightedEntryIndex);
        }

        public void HighlightPreviousEntry()
        {
            _HighlightedEntryIndex--;
            if (_HighlightedEntryIndex < 0)
                _HighlightedEntryIndex = _Items.Count - 1;
            HighlightEntry(_HighlightedEntryIndex);
        }

        public virtual void Update(GameTime gameTime)
        {
            double currentTime = gameTime.TotalGameTime.TotalMilliseconds;

            if (_TransitionStartAt == NOT_INIT)
            {
                _TransitionStartAt = currentTime;
            }

            switch (_State)
            {
                case ScreenStates.TransitionIn:
                    _TransitionProgress = (float)((currentTime - _TransitionStartAt) / _TransitionInDuration);
                    _Scale = ((1f - _TransitionProgress) * 20f) + 1f;
                    _Alpha = Math.Min((byte)(_TransitionProgress * 255),(byte)255);
                    break;

                case ScreenStates.TransitionOut:
                    _TransitionProgress = (float)((currentTime - _TransitionStartAt) / _TransitionOutDuration);
                    _Scale = (1f - _TransitionProgress);
                    _Alpha = Math.Min((byte)(_TransitionProgress * 255), (byte)255);
                    break;

                case ScreenStates.TransitionFinished:
                    break;
            }

            if (_TransitionProgress >= 1.0f)
                ChangeState(ScreenStates.TransitionFinished, gameTime);
        }

        public void ChangeState(ScreenStates state, GameTime time)
        {
            _State = state;
            _TransitionStartAt = time.TotalGameTime.TotalMilliseconds;
            _TransitionProgress = 0.0f;
        }
        public virtual void Draw(GameTime gameTime)
        {
            _SpriteBatch.Begin();
            _SpriteBatch.DrawString(_Font, _Title, _TitlePosition, Color.White);

            int i = 0;
            Color color;
            foreach (MenuItem item in _Items)
            {
                if (item.IsHightlighted)
                    color = Color.Red;
                else
                    color = Color.White;

                _SpriteBatch.DrawString(_Font, item.Name, _TitlePosition + _TitleMarginBottom + _ItemMarginBottom * i, new Color(color.R,color.G,color.B,_Alpha), _Rot, Vector2.Zero, _Scale, SpriteEffects.None, 0);
                i++;
            }
            _SpriteBatch.End();
        }

        public virtual void LoadContent()
        {
            _Font = BaseGame.GetInstance().Content.Load<SpriteFont>("Fonts/Yikes!14");
        }

        public virtual void UnloadContent()
        {
        }
    }
}
